<?php
namespace Home\Controller;
use Think\Controller;
include_once("Common.php");
include_once("mysqlha.php");
include_once("UpFileTool.class.php");
// 用于显示主页面
class BuyGroupController extends Controller
{
    
	//显示拼团专区
	public function showList(){

		$page = $_REQUEST["pageNum"];
		$pagecount = $_REQUEST['numPerPage'];
		$keywords = $_REQUEST['keywords'];
		if($page == null||$page<=1){
			$page=1;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount=20;
		}
		$Model=M();
		$startcount = ($page-1)*$pagecount;
		if($keywords == "") {
			$result = $Model->Query("select * from buygroup order by addtime desc limit ".$start.",".$pagecount."");
			$count = $Model->Query("select count(*) as count FROM buygroup");
		}else{
		}
		$param = array(
			"count"=>$count[0]['count'],
			"numPerPage"=> $pagecount,
			"pageNum"=>$page
		);
		$this->assign('param',$param);
		$this->assign('resultList',$result);
		$this->display("Admin/buygroupList.php");
	}
	
	//修改分类页面
    public function changeOne(){
        $Model = M();
        $id = $_REQUEST['id'];
		$Obj = $Model->Query("select * from buygroup where buygroup_id=".$id."");
		$this->assign('Obj',$Obj[0]);
        $this->display('Admin/changebuyGroup.php');
    }

    //修改分类
    public function OneChange(){
        $name = $_REQUEST['name'];
        $buygroup_id = $_REQUEST['id'];
		$sort = $_REQUEST['sort'];
		$mcount = $_REQUEST['mcount'];
		$money = $_REQUEST['money'];
        
        $updateArr = array();
		$updateArr['name'] = $name;
		$updateArr['sort'] = $sort;
		$updateArr['money'] = $money;
		$updateArr['mcount'] = $mcount;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
            if($images!=""){
				$updateArr['image'] = $images;
				$updateArr['sumbimage'] = $sumbimages;
			}
        }
		update('buygroup',$updateArr,"buygroup_id=".$circle_id."");
		$returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroup_list",
			"forwardUrl"=>"index.php?c=buyGroup&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
	public function ChangeSort(){
		$sort = $_REQUEST['sort'];
		$id = $_REQUEST['id'];

		$updateArr = array(
		    "sort"=>$sort
		);
		update('buygroup',$updateArr,"buygroup_id=".$id."");
		$returnArr = array(
			"code"=>200,
			"forwardUrl"=>'index.php?c=buyGroup&a=showList',
			"msg"=>"更新成功"
		);
		echo json_encode($returnArr);

	}



    //添加拼团专区
    public function showaddOne(){
        $this->display('Admin/addbuyGroup.php');
    }

    //添加拼团专区
    public function addOne(){
        $mcount = $_REQUEST['mcount'];
		$money = $_REQUEST['money'];
		$name = $_REQUEST['name'];
        $insertArr['mcount'] = $mcount;
		$insertArr['money'] = $money;
		$insertArr['name'] = $name;
		$insertArr['sort'] = $sort;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
             
            $insertArr['sumbimage'] = $sumbimages;
			$insertArr['image'] = $sumbimages;
            
        }
        
		myinsert('buygroup',$insertArr);
        $returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroup_list",
			"forwardUrl"=>"index.php?c=buyGroup&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
}